//
//  MainTabBarController.m
//  TaskManager
//
//  Created by Sun on 2019/3/1.
//  Copyright © 2019 Sun. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainNavigationController.h"
#import "HomeViewController.h"
#import "LadingBillViewController.h"
#import "MineViewController.h"
#import "TranManagerVC.h"
#import "StorageManagerVC.h"
#import "PackageManagerVC.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabBarController

+ (instancetype)instance{
    UIViewController *vc = [[[UIApplication sharedApplication] delegate]window].rootViewController;
    if ([vc isKindOfClass:[MainTabBarController class]]) {
        return (MainTabBarController *)vc;
    }else{
        return nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color_backgroundColor;
    
    HomeViewController *homeVC = [HomeViewController new];
    [self createTabBarWithChildVC:homeVC andItem:@"首页" normalImage:@"tab_home" selectedImage:@"tab_home_sel"];
    TranManagerVC *tranManagerVC = [TranManagerVC new];
    [self createTabBarWithChildVC:tranManagerVC andItem:@"运输管理" normalImage:@"tab_waybill" selectedImage:@"tab_waybill_sel"];
//    StorageManagerVC *storageManagerVC = [StorageManagerVC new];
//    [self createTabBarWithChildVC:storageManagerVC andItem:@"仓储管理" normalImage:@"tab_storage" selectedImage:@"tab_storage_sel"];
//    PackageManagerVC *packageManagerVC = [PackageManagerVC new];
//    [self createTabBarWithChildVC:packageManagerVC andItem:@"包装管理" normalImage:@"tab_packing" selectedImage:@"tab_packing_sel"];
    MineViewController *mineVC = [MineViewController new];
    [self createTabBarWithChildVC:mineVC andItem:@"我的" normalImage:@"tab_my" selectedImage:@"tab_my_sel"];
    self.delegate = self;
    self.selectedIndex = 0;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
}

/**
 *  自定义底部tabbar按方法
 *
 *  @param title       tabbar按钮的标题
 *  @param normalImg   未选择时候的图标
 *  @param selectedImg 选中时候的图标
 */
-(void)createTabBarWithChildVC:(UIViewController *)vc andItem:(NSString *)title normalImage:(NSString *)normalImg selectedImage:(NSString *)selectedImg {
    MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:vc];
//    NSDictionary *navTitleDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0xffffff),NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
//    [vc.navigationController.navigationBar setTitleTextAttributes:navTitleDic];
    UIImage *myImage = [UIImage imageNamed:normalImg];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = myImage;
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImg];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = mySelectedImage;
//    [vc.tabBarItem setImageInsets:UIEdgeInsetsMake(-2, 2, 2, 0)];
    vc.tabBarItem.title = title;
    NSDictionary *titleSelectedDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x3D3E5A),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    NSDictionary *titleNormalDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x9E9FB5),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    [vc.tabBarItem setTitleTextAttributes:titleNormalDic forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:titleSelectedDic forState:UIControlStateSelected];
    [self addChildViewController:nav];
}

- (void)dealloc {
    NSLog(@"---- %@ ---- dealloc ----",self.class);
}

@end
